
package com.trydofor.a9task.base.code.help;

import com.trydofor.a9task.base.code.bean.CodeLangRw;
import com.trydofor.a9task.base.code.bean.CodeMainRw;
import com.trydofor.a9task.base.code.sysd.Activity;
import com.trydofor.aloe.exception.Exceptions;

/**
 * one builder on code
 * 
 * @author : Shi Rongjiu (www.trydofor.com)
 * @date : Nov 7, 2012
 * @see :
 */

public final class CodeBuilder {
    
    public static CodeBuilder code(int gid, int id, String key) {
        return new CodeBuilder(gid, id, key);
    }
    
    
    private final Code code;
    private transient boolean    isDone = false;
    
    private CodeBuilder(int gid, int id, String key) {
        CodeMainRw main = new CodeMainRw();
        // initial
        main.setGroupId(gid);
        main.setId(id);
        main.setCodeKey(key);
        // default
        main.setOrderBy(id);
        main.setActivity(Activity.$ACTIVE);
        code = new Code(main);
    }
    
    public CodeBuilder orderBy(int orderBy) {
        Exceptions.checkIllegalState(isDone, "can not set when it's done");
        ((CodeMainRw) code.main()).setOrderBy(orderBy);
        return this;
    }
    
    public CodeBuilder activity(int activity) {
        Exceptions.checkIllegalState(isDone, "can not set when it's done");
        ((CodeMainRw) code.main()).setActivity(activity);
        return this;
    }
    
    public CodeBuilder lang(int lang, String text) {
        return lang(lang,text,text);
    }
    
    public CodeBuilder lang(int lang, String text, String info) {
        Exceptions.checkIllegalState(isDone, "can not set when it's done");
        CodeLangRw cl = new CodeLangRw();
        cl.setCodeId(code.main().getId());
        cl.setLanguage(lang);
        cl.setCodeText(text);
        cl.setCodeInfo(info);
        code.lang(cl);
        return this;
    }
    
    public Code done() {
        isDone = true;
        return code;
    }
}
